import 'package:flutter/material.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/default_empty_widget.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/default_fail_widget.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/default_error_widget.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/default_loading_widget.dart';

class MultiStateWidget extends StatefulWidget {
  final MultiState state;
  final MultiModel model;
  final MultiEmptyType emptyType;
  final Widget child;
  final Widget? loadingWidget;
  final Widget? emptyWidget;
  final Widget? failWidget;
  final Widget? errorWidget;

  final RetryCallback? onRetry;

  MultiStateWidget({
    Key? key,
    this.state = MultiState.loading,
    this.model = MultiModel.normal,
    this.emptyType = MultiEmptyType.normal,
    required this.child,
    this.loadingWidget,
    this.emptyWidget,
    this.failWidget,
    this.errorWidget,
    this.onRetry,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MultiStateWidgetState();
  }
}

class _MultiStateWidgetState extends State<MultiStateWidget> {
  @override
  Widget build(BuildContext context) {
    switch (widget.state) {
      case MultiState.loading:
        return loadingWidget;
      case MultiState.success:
        return widget.child;
      case MultiState.empty:
        return emptyWidget;
      case MultiState.fail:
        return failWidget;
      case MultiState.error:
        return errorWidget;
      default:
        return widget.child;
    }
  }

  Widget get loadingWidget {
    return widget.loadingWidget != null ? widget.loadingWidget! : DefaultLoadingWidget();
  }

  Widget get emptyWidget {
    return widget.emptyWidget != null ? widget.emptyWidget! : DefaultEmptyWidget(model: widget.model, emptyType: widget.emptyType);
  }

  Widget get failWidget {
    return widget.failWidget != null ? widget.failWidget! : DefaultFailWidget(model: widget.model);
  }

  Widget get errorWidget {
    return widget.errorWidget != null ? widget.errorWidget! : DefaultErrorWidget(model: widget.model, onRetry: widget.onRetry);
  }
}

enum MultiState {
  loading,
  success,
  empty,
  fail,
  error,
}

enum MultiModel {
  //正常
  normal,
  //浅色
  tint,
}

enum MultiEmptyType {
  //正常
  normal,
  //记录
  record,
  //收藏
  collect,
  //定位
  location,
  //消息
  message,
}
